﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;


namespace iEN.PlatformSet
{
    public partial class EventLog : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            DataTable dtOrg = new DBAccess().getOrgGroupTable("1");
            Session["dtOrg"] = dtOrg;

            if (!Page.IsPostBack)
            {
                if (Session["SysAdmin"] != null)
                {
                    this.Intialize();
                    
                }
                else
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }       
            }

            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                lblMsg.Visible = true;
            }
        }

        #region Initialization methods
        private void Intialize()
        {
            ddBuild.Items.Clear();
            ddBuild.Items.Add(new ListItem(string.Empty, "0"));

            DataTable dtOrgGroup = new DBAccess().getOrgGroupTable("1");
            DataTable dtOrgGroupTemp = dtOrgGroup.Copy();
            Session["dtOrgGroup"] = dtOrgGroup;

            dtOrgGroupTemp.DefaultView.RowFilter = "_level=1";
            dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

            for (int i = 0; i < dtOrgGroupTemp.Rows.Count; i++)
            {
                this.ddBuild.Items.Add(new ListItem(dtOrgGroupTemp.Rows[i]["_name"].ToString(), dtOrgGroupTemp.Rows[i]["_id"].ToString()));
            }

            ddlCategory.Items.Clear();
            ddlCategory.Items.Add(new ListItem(string.Empty, "0"));

            DataTable dtEvent = new DBAccess().getEventTable();
            DataTable dtEventTemp = dtEvent.Copy();
            Session["dtEvent"] = dtEvent;

            dtEventTemp = dtEventTemp.DefaultView.ToTable(true, "_category");

            for (int i = 0; i < dtEventTemp.Rows.Count; i++)
            {
                this.ddlCategory.Items.Add(new ListItem(dtEventTemp.Rows[i]["_category"].ToString(), dtEventTemp.Rows[i]["_category"].ToString()));
            }

        }
        #endregion

        private void GetEventLogList()
        {
            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                lblMsg.Visible = true;
            }
            DataTable dtEventLog = new DataTable();

            if (ddType.SelectedIndex == -1)
            {
                dtEventLog = new DBAccess().getEventLogTable(ddlCategory.SelectedItem.Text,
                    "", tbStartTime.Text, tbEndTime.Text);
            }
            else
            {
                dtEventLog = new DBAccess().getEventLogTable(ddlCategory.SelectedItem.Text,
                    ddType.SelectedItem.Text, tbStartTime.Text, tbEndTime.Text);
            }

            Session["dtEventLog"] = dtEventLog;
            dtEventLog.Columns.Add("build", typeof(String));
            dtEventLog.Columns.Add("floor", typeof(String));
            dtEventLog.Columns.Add("block", typeof(String));

            for (int i = 0; i < dtEventLog.Rows.Count; i++)
            {
                if (dtEventLog.Rows[i]["_org_id"].ToString() != "" && dtEventLog.Rows[i]["_org_id"].ToString() != "&nbsp;")
                {
                    string[] strOrg = getOrgHierarchy(dtEventLog.Rows[i]["_org_id"].ToString());
                    dtEventLog.Rows[i].SetField("build", strOrg[0]);
                    dtEventLog.Rows[i].SetField("floor", strOrg[1]);
                    dtEventLog.Rows[i].SetField("block", strOrg[2]);
                }

                if (dtEventLog.Rows[i]["_org_id"].ToString() == "" || dtEventLog.Rows[i]["_org_id"].ToString() == "&nbsp;")
                {
                    dtEventLog.Rows[i].SetField("build", "");
                    dtEventLog.Rows[i].SetField("floor", "");
                    dtEventLog.Rows[i].SetField("block", "");
                }

                if (ddBlock.SelectedIndex != -1 && ddBlock.SelectedIndex != 0)
                {
                    if (ddBlock.SelectedItem.Text != dtEventLog.Rows[i]["block"].ToString())
                        dtEventLog.Rows[i].Delete();

                }
                else if (ddfloor.SelectedIndex != -1 && ddfloor.SelectedIndex != 0)
                {
                    if (ddfloor.SelectedItem.Text != dtEventLog.Rows[i]["floor"].ToString())
                        dtEventLog.Rows[i].Delete();
                }
                else if (ddBuild.SelectedIndex != -1 && ddBuild.SelectedIndex != 0)
                {
                    if (ddBuild.SelectedItem.Text != dtEventLog.Rows[i]["build"].ToString())
                        dtEventLog.Rows[i].Delete();
                }


            }
            
            gvEventLog.DataSource = dtEventLog;
            gvEventLog.DataBind();
            gvEventLog.Visible = true;

        }

        protected void gvEventLog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {


                if (e.Row.Cells[9].Text == "1")
                    e.Row.Cells[9].Text = "Y";
                else if (e.Row.Cells[9].Text == "0")
                    e.Row.Cells[9].Text = "N";



            }
            
        }


        protected string[] getOrgHierarchy(string strOrgID)
        {
            string[] strRet = new string[] { "", "", "" };
            DataTable dtOrg = (DataTable)Session["dtOrg"];
            DataTable dtFloor = dtOrg.Copy();
            DataTable dtBuild = dtOrg.Copy();
            DataTable dtOrgCopy = dtOrg.Copy();
            dtOrgCopy.DefaultView.RowFilter = "_id = " + strOrgID;
            dtOrgCopy = dtOrgCopy.DefaultView.ToTable();

            if (dtOrgCopy == null || dtOrgCopy.Rows.Count < 1)
                return strRet;

            if (dtOrgCopy.Rows[0]["_level"].ToString() == "1")
            {
                strRet[0] = dtOrgCopy.Rows[0]["_name"].ToString();
                return strRet;
            }

            if (dtOrgCopy.Rows[0]["_level"].ToString() == "2")
            {
                strRet[1] = dtOrgCopy.Rows[0]["_name"].ToString();

                dtFloor.DefaultView.RowFilter = "_id=" + dtOrgCopy.Rows[0]["_parent_id"].ToString();
                dtFloor = dtFloor.DefaultView.ToTable();

                if (dtFloor == null || dtFloor.Rows.Count < 1)
                    return strRet;

                strRet[0] = dtFloor.Rows[0]["_name"].ToString();

                return strRet;
            }

            if (dtOrgCopy.Rows[0]["_level"].ToString() == "3")
            {
                strRet[2] = dtOrgCopy.Rows[0]["_name"].ToString();

                dtFloor.DefaultView.RowFilter = "_id=" + dtOrgCopy.Rows[0]["_parent_id"].ToString();
                dtFloor = dtFloor.DefaultView.ToTable();

                if (dtFloor == null || dtFloor.Rows.Count < 1)
                    return strRet;

                strRet[1] = dtFloor.Rows[0]["_name"].ToString();

                dtBuild.DefaultView.RowFilter = "_id=" + dtFloor.Rows[0]["_parent_id"].ToString();
                dtBuild = dtBuild.DefaultView.ToTable();

                if (dtBuild == null || dtBuild.Rows.Count < 1)
                    return strRet;

                strRet[0] = dtBuild.Rows[0]["_name"].ToString();

                return strRet;
            }


            return strRet;
        }

        protected void ddBuild_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddBuild.SelectedValue!="0")
            {
                ddfloor.Items.Clear();
                ddfloor.Items.Add(new ListItem(string.Empty, "0"));

                DataTable dtOrgGroup = (DataTable)Session["dtOrgGroup"];
                DataTable dtOrgGroupTemp = dtOrgGroup.Copy();

                dtOrgGroupTemp.DefaultView.RowFilter = "_level=2 and _parent_id=" + ddBuild.SelectedValue;
                dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

                for (int i = 0; i < dtOrgGroupTemp.Rows.Count; i++)
                {
                    this.ddfloor.Items.Add(new ListItem(dtOrgGroupTemp.Rows[i]["_name"].ToString(), dtOrgGroupTemp.Rows[i]["_id"].ToString()));
                }
            }

        }

        protected void ddfloor_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddfloor.SelectedValue != "0")
            {
                ddBlock.Items.Clear();
                ddBlock.Items.Add(new ListItem(string.Empty, "0"));

                DataTable dtOrgGroup = (DataTable)Session["dtOrgGroup"];
                DataTable dtOrgGroupTemp = dtOrgGroup.Copy();

                dtOrgGroupTemp.DefaultView.RowFilter = "_level=3 and _parent_id=" + ddfloor.SelectedValue;
                dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

                for (int i = 0; i < dtOrgGroupTemp.Rows.Count; i++)
                {
                    this.ddBlock.Items.Add(new ListItem(dtOrgGroupTemp.Rows[i]["_name"].ToString(), dtOrgGroupTemp.Rows[i]["_id"].ToString()));
                }
            }
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCategory.SelectedValue != "0")
            {
                ddType.Items.Clear();
                ddType.Items.Add(new ListItem(string.Empty, "0"));

                DataTable dtEvent = (DataTable)Session["dtEvent"];
                DataTable dtEventTemp = dtEvent.Copy();

                dtEventTemp.DefaultView.RowFilter = "_category ='" + ddlCategory.SelectedValue + "'";
                dtEventTemp = dtEventTemp.DefaultView.ToTable();

                for (int i = 0; i < dtEventTemp.Rows.Count; i++)
                {
                    this.ddType.Items.Add(new ListItem(dtEventTemp.Rows[i]["_event_type"].ToString(), dtEventTemp.Rows[i]["_id"].ToString()));
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            GetEventLogList();
            
        }

        protected void gvEventLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvEventLog.DataSource = (DataTable)Session["dtEventLog"];
            this.gvEventLog.PageIndex = e.NewPageIndex;
            this.gvEventLog.DataBind();

        }


    }
}